package br.com.progepe.dao;

import java.security.NoSuchAlgorithmException;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.progepe.encripty.Encripty;
import br.com.progepe.entity.Pessoa;
import br.com.progepe.util.HibernateUtility;

public class PessoaDAO extends DAO {

	private static PessoaDAO instance;

	public static PessoaDAO getInstance() {
		if (instance == null) {
			instance = new PessoaDAO();
		}
		return instance;
	}

	public Pessoa autentica(Pessoa pessoa) throws NoSuchAlgorithmException {
		HibernateUtility.beginTransaction();
		Criteria c = HibernateUtility.getSession().createCriteria(Pessoa.class);
		c.add(Restrictions.eq("cpf", pessoa.getCpf()));
		c.add(Restrictions.eq("senha",
				Encripty.criptografaSenha(pessoa.getSenha())));
		HibernateUtility.commitTransaction();
		return (Pessoa) c.uniqueResult();
	}

}
